/*
 * Copyright (c) 2023, 2024,yzc.cn All rights reserved.
 *
 */
package com.by.common.utils;

import net.sourceforge.pinyin4j.PinyinHelper;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * <p>Project: erp-cloud-api - AutoCode</p>
 * <p>Powered by yzc On 2024-01-10 15:44:21</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
public class AutoCode {
    public static String autoCodePrefix(String hanzi) {

        StringBuilder sb = new StringBuilder();
        for (char ch : hanzi.toCharArray()) {
            // 判断是否为汉字字符
            if (Character.toString(ch).matches("[\\u4E00-\\u9FA5]+")) {
                // 使用pinyin4j库获取汉字的拼音数组
                String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(ch);
                // 取第一个拼音的首字母并转换为大写
                String firstLetter = Character.toString(pinyinArray[0].charAt(0)).toUpperCase();
                sb.append(firstLetter);
            } else {
                sb.append(ch);
            }
        }
        //获得大写首字母
        String prefix = sb.toString();
        return prefix;
    }

    public static String getNewCode(String maxCode, String front) {
        String newCode = "";
        if (maxCode == null) {
            //初始化code  格式：前缀+年份月份+序号
            newCode = front  + "000001";
        } else {
            //获得MaxCode的前缀
            String prefix = maxCode.substring(0, maxCode.length() - 6);
            //获得MaxCode的后缀
            String suffix = maxCode.substring(maxCode.length() - 6);
            //后缀加1
            String newSuffix = String.format("%06d", Integer.parseInt(suffix) + 1);
            //前缀加后缀组成新的code
            newCode = prefix + newSuffix;
        }
        return newCode;
    }
}
